using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PengYouClient
{
    public partial class ReportProgressBar : Form
    {
        public OpenVersion openversion;
        Thread h_bar;

        delegate void IncrementState();
        delegate void CloseWindow();
        IncrementState d_IncrementState;
        CloseWindow d_CloseWindow;

        public ReportProgressBar(OpenVersion openversion)
        {
            this.openversion = openversion;
            d_IncrementState = new IncrementState(this.IncrementStateMethod);
            d_CloseWindow = new CloseWindow(this.CloseWindowMethod);
            InitializeComponent();
        }

        public void IncrementStateMethod()
        {
            this.ReportPgsBar.Increment(1);
        }

        public void CloseWindowMethod()
        {
            this.h_bar.Abort();
            this.Close();      
        }

        public class IncrementProgressBar
        {
            OpenVersion openversion;
            ReportProgressBar report;

            public IncrementProgressBar(OpenVersion openversion, ReportProgressBar report)
            {
                this.openversion = openversion;
                this.report = report;
            }

            public void IncrementProgressBarRun()
            {
                while (this.openversion.getVersion == false && this.openversion.errGetVersion == false)
                {
                    Thread.Sleep(1000);
                    this.report.Invoke(report.d_IncrementState);
                }
                this.report.Invoke(report.d_CloseWindow);
            }
        }

        private void ReportProgressBar_Load(object sender, EventArgs e)
        {
            Text = LanguageManager.GetString("WindowReportProgressBarRetrieving");
            Cancel.Text = LanguageManager.GetString("WindowReportProgressBarCancel");
            IncrementProgressBar bar = new IncrementProgressBar(this.openversion, this);
            this.h_bar = new Thread(new ThreadStart(bar.IncrementProgressBarRun));
            this.h_bar.Start();
        }
    }
}